﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using BlogEngine.Core;

public partial class admin_BrowseAttachments : System.Web.UI.Page
{
	private int _page;
	private int _attachmentId;

	/// <summary>
	/// Handles the PreInit event of the Page control.
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void Page_PreInit (object sender, EventArgs e) 
	{
		string qpage = Request.QueryString["page"];
		string qattachmentId = Request.QueryString["id"];

		if (!Int32.TryParse(qpage, out _page))
			_page = 1;

		if (!Int32.TryParse(qattachmentId, out _attachmentId))
			_attachmentId = -1;
	}

	/// <summary>
	/// Handles the Load event of the Page control.
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void Page_Load(object sender, EventArgs e)
	{
		DataBind();
	}

	/// <summary>
	/// Binds a data source to the invoked server control and all its child controls.
	/// </summary>
	public override void DataBind()
	{
		backwardLink.Visible = false;
		forwardLink.Visible = false;

		// if no attachment id is selected 
		if (_attachmentId == -1)
		{
			var attachments = Attachment.GetAttachments(((_page - 1) * 10), 10);
			attachmentRepeater.DataSource = attachments;
			attachmentRepeater.DataBind();

			if (_page > 1)
			{
				backwardLink.Visible = true;
				backwardLink.NavigateUrl = "browse-attachments.aspx?page=" + (_page - 1);
			}

			if (attachments.Count == 10)
			{
				forwardLink.Visible = true;
				forwardLink.NavigateUrl = "browse-attachments.aspx?page=" + (_page + 1);
			}
		}
		else
		{
			backwardLink.Visible = true;
			backwardLink.NavigateUrl = "browse-attachments.aspx?page=" + _page;

			attachmentRepeater.Visible = false;
		}
	}

	/// <summary>
	/// Gets the attachment.
	/// </summary>
	/// <param name="a">A.</param>
	/// <returns></returns>
	protected Control GetAttachment(Attachment a)
	{
		if (a.MimeType.StartsWith("image"))
		{
			ImageButton link = new ImageButton();
			link.PostBackUrl = "browse-attachments.aspx?page=" + _page + "&id=" + a.Id.ToString();
			link.ImageUrl = a.PermaLink.ToString();
			return link;
		}
		else
		{
			LinkButton link = new LinkButton();
			link.PostBackUrl = "browse-attachments.aspx?page=" + _page + "&id="+ a.Id.ToString();
			link.Text = a.Title;
			return link;
		}
	}

	/// <summary>
	/// Handles the ItemDataBound event of the attachmentRepeater control.
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterItemEventArgs"/> instance containing the event data.</param>
	protected void attachmentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
		{
			e.Item.Controls.Add(new LiteralControl("<li id=\"attachment-" + (e.Item.DataItem as Attachment).Id + "\">"));
			e.Item.Controls.Add(GetAttachment(e.Item.DataItem as Attachment));
			e.Item.Controls.Add(new LiteralControl("</li>"));
		}
	}
}
